package com.moli.iov.admin.service.impl;

import com.moli.iov.admin.model.vo.req.UserVo;
import com.moli.iov.admin.service.ILoginLogFacadeService;
import com.moli.iov.sys.model.dto.res.LoginDto;
import com.moli.iov.sys.model.po.mongodb.LoginLog;
import com.moli.iov.sys.remote.ILoginLogRemoteService;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.IpUtil;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * <p>
 *
 * </p>
 *
 * @author zql
 * @since 2019/9/26 16:41
 */
@Slf4j
@Service
public class LoginLogServiceFacadeImpl implements ILoginLogFacadeService {

  @Reference(group = "${service.group}", check = false)
  private ILoginLogRemoteService loginLogRemoteService;

  /**
   * 添加
   * @param userVo
   * @param loginDto
   * @param ist
   */
  @Async
  @Override
  public void save(UserVo userVo,  LoginDto loginDto, boolean ist, HttpServletRequest request) {
    LoginLog loginLog = new LoginLog();
    if(loginDto != null && loginDto.getUser() != null){
      CommonUtils.copyProperties(loginDto.getUser(), loginLog);
    }
    loginLog.setAccount(userVo.getAccount());
    loginLog.setResult(ist);
    loginLog.setIp(IpUtil.getIpAddrByRequest(request));
    loginLog.setLoginTime(new Date());
    loginLogRemoteService.save(loginLog);
    log.info("添加登录日志");
  }
}
